﻿using System.Diagnostics;
using System.Windows.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Keyboard = Microsoft.Xna.Framework.Input.Keyboard;

namespace Silverlight3dApp.Learning.Lesson02
{
    public class CollisionGame : Game
    {
        private SpriteBatch _spriateBatch;

        private Texture2D _textureThreeRings;
        private Texture2D _textureSkullBall;

        private Vector2 _threeRingsFrameSize = new Vector2(75, 75);
        private Vector2 _threeRingsCurrentFrame = new Vector2(0, 0);
        private Vector2 _threeRingsSheetSize = new Vector2(6, 8);

        private int _threeRingsTimeSinceLastFrame;
        private int _threeRingsMillisecondsPerFrame = 50;

        private Vector2 _threeRingsPosition = Vector2.Zero;
        private float _threeRingsSpeed = 6.0f;

        private int _threeRingsRectOffset = 10;

        private Vector2 _skullFrameSize = new Vector2(75, 75);
        private Vector2 _skullCurrentFrame = new Vector2(0, 0);
        private Vector2 _skullSheetSize = new Vector2(6, 8);

        private int _skullTimeSinceLastFrame;
        private int _skullMillisecondsPerFrame = 50;

        private Vector2 _skullPosition = new Vector2(100, 100);
        private Vector2 _skullSpeed = new Vector2(5f, 5f);

        private int _skullRectOffset = 10;


        private MouseState _preMouseState;

        public CollisionGame()
        {
            Content.RootDirectory = "Content";
        }

        protected override void LoadContent()
        {
            _spriateBatch = new SpriteBatch(GraphicsDevice);
            _textureThreeRings = Content.Load<Texture2D>(@"Images/threerings");
            _textureSkullBall = Content.Load<Texture2D>(@"Images/skullball");
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            _spriateBatch = null;
            base.UnloadContent();
        }

        protected bool Collide()
        {
            var threeRingsRect = new Rectangle((int) _threeRingsPosition.X + _threeRingsRectOffset, (int) _threeRingsPosition.Y + _threeRingsRectOffset, (int) _threeRingsFrameSize.X - (_threeRingsRectOffset * 2), (int) _threeRingsFrameSize.Y - (_threeRingsRectOffset * 2));
            var skullRect = new Rectangle((int) _skullPosition.X + _skullRectOffset, (int) _skullPosition.Y + _skullRectOffset, (int) _skullFrameSize.X - (_skullRectOffset * 2), (int) _skullFrameSize.Y - (_skullRectOffset * 2));
            return threeRingsRect.Intersects(skullRect);
        }

        protected override void Update(GameTime gameTime)
        {
            var keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Key.Escape))
            {
                Exit();
            }

            _threeRingsTimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (_threeRingsTimeSinceLastFrame > _threeRingsMillisecondsPerFrame)
            {
                _threeRingsTimeSinceLastFrame -= _threeRingsMillisecondsPerFrame;
                ++_threeRingsCurrentFrame.X;
                if (_threeRingsCurrentFrame.X >= _threeRingsSheetSize.X)
                {
                    _threeRingsCurrentFrame.X = 0;
                    ++_threeRingsCurrentFrame.Y;
                    if (_threeRingsCurrentFrame.Y >= _threeRingsSheetSize.Y)
                    {
                        _threeRingsCurrentFrame.Y = 0;
                    }
                }
            }

            _skullTimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (_skullTimeSinceLastFrame > _skullMillisecondsPerFrame)
            {
                _skullTimeSinceLastFrame -= _skullMillisecondsPerFrame;
                ++_skullCurrentFrame.X;
                if (_skullCurrentFrame.X >= _skullSheetSize.X)
                {
                    _skullCurrentFrame.X = 0;
                    ++_skullCurrentFrame.Y;
                    if (_skullCurrentFrame.Y >= _skullSheetSize.Y)
                    {
                        _skullCurrentFrame.Y = 0;
                    }
                }
            }

            _skullPosition += _skullSpeed;
            if (_skullPosition.X > (GraphicsDevice.Viewport.Width - _skullFrameSize.X) || _skullPosition.X < 0)
            {
                _skullSpeed.X *= -1;
            }
            if (_skullPosition.Y > (GraphicsDevice.Viewport.Height - _skullFrameSize.Y) || _skullPosition.Y < 0)
            {
                _skullSpeed.Y *= -1;
            }

            var keyboardState = Keyboard.GetState();
            if (keyboardState.IsKeyDown(Key.A))
            {
                if (keyboardState.IsKeyDown(Key.Left))
                {
                    _threeRingsPosition.X -= _threeRingsSpeed * 2;
                }
                if (keyboardState.IsKeyDown(Key.Right))
                {
                    _threeRingsPosition.X += _threeRingsSpeed * 2;
                }
                if (keyboardState.IsKeyDown(Key.Up))
                {
                    _threeRingsPosition.Y -= _threeRingsSpeed * 2;
                }
                if (keyboardState.IsKeyDown(Key.Down))
                {
                    _threeRingsPosition.Y += _threeRingsSpeed * 2;
                }
            }
            else
            {
                if (keyboardState.IsKeyDown(Key.Left))
                {
                    _threeRingsPosition.X -= _threeRingsSpeed;
                }
                if (keyboardState.IsKeyDown(Key.Right))
                {
                    _threeRingsPosition.X += _threeRingsSpeed;
                }
                if (keyboardState.IsKeyDown(Key.Up))
                {
                    _threeRingsPosition.Y -= _threeRingsSpeed;
                }
                if (keyboardState.IsKeyDown(Key.Down))
                {
                    _threeRingsPosition.Y += _threeRingsSpeed;
                }
            }

            var mouseState = Mouse.GetState();
            if (mouseState.X != _preMouseState.X || mouseState.Y != _preMouseState.Y)
            {
                _threeRingsPosition = new Vector2(mouseState.X, mouseState.Y);
                _preMouseState = mouseState;
            }

            if (_threeRingsPosition.X < 0)
            {
                _threeRingsPosition.X = 0;
            }
            if (_threeRingsPosition.Y < 0)
            {
                _threeRingsPosition.Y = 0;
            }
            var maxWidth = GraphicsDevice.Viewport.Width - _threeRingsFrameSize.X;
            if (_threeRingsPosition.X > maxWidth)
            {
                _threeRingsPosition.X = maxWidth;
            }
            var maxHeight = GraphicsDevice.Viewport.Height - _threeRingsFrameSize.Y;
            if (_threeRingsPosition.Y > maxHeight)
            {
                _threeRingsPosition.Y = maxHeight;
            }

            if (Collide())
            {
                Debug.WriteLine("Collide!");
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Colors.CornflowerBlue);
            _spriateBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
            _spriateBatch.Draw(_textureThreeRings, _threeRingsPosition, new Rectangle((int) (_threeRingsCurrentFrame.X * _threeRingsFrameSize.X), (int) (_threeRingsCurrentFrame.Y * _threeRingsFrameSize.Y), (int) _threeRingsFrameSize.X, (int) _threeRingsFrameSize.Y), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1f);
            _spriateBatch.Draw(_textureSkullBall, _skullPosition, new Rectangle((int) (_skullCurrentFrame.X * _skullFrameSize.X), (int) (_skullCurrentFrame.Y * _skullFrameSize.Y), (int) _skullFrameSize.X, (int) _skullFrameSize.Y), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1f);

            _spriateBatch.End();
            base.Draw(gameTime);
        }
    }
}